package com.google.code.sprite.generate;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import com.google.code.sprite.conf.SpriteConfig;
import com.google.code.sprite.conf.TableConfig;
import com.google.code.sprite.util.Util;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.commons.io.FilenameUtils;


public abstract class AbstractGenerate implements BasicGenerate {

    protected TableMetaData tableMetaData;
    protected TableConfig tableConfig;

    protected String pkg ;
    protected String rootPkg;
    protected String actionClass ;
    protected String serviceClass;
    protected String serviceImplClass;
    protected String daoClass;
    protected String daoImplClass;
    protected String entityClass;


    protected String entityVar;
    protected String serviceVar;
    protected String daoVar;

    private String getSubTargetPath(String subType ) {

       String subPkg = this.getSubPackage();
        String pkgRtlPath = tableConfig.getPkgName().replace(".","/");
        String subPkgRtlPath = subPkg.replace(".","/");
        SpriteConfig spriteConfig = SpriteConfig.getInstance();

        String targetDir = spriteConfig.getJavaTargetDir();
        String pkgAbsPath =  FilenameUtils.concat(targetDir,pkgRtlPath);
        File pkgFile = new File(FilenameUtils.concat(pkgAbsPath, subPkgRtlPath));

        if (subType.equals("web") ) {
            targetDir = spriteConfig.getWebTargetDir();
            pkgFile = new File(FilenameUtils.concat(targetDir, subPkgRtlPath));
        } 
     
        if (!pkgFile.exists()) {
            pkgFile.mkdirs();
        }
        return pkgFile.getAbsolutePath();
    }

    public String getWebSubPackagePath() {
        return getSubTargetPath("web");
    }
    public String getJavaSubPackagePath() {
        return getSubTargetPath("java");
    }

    public static void writeToFile(String filePath, String content) {
        try {
            PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"));
            pw.print(content);
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    protected abstract String getSubPackage() ;

    public void init(TableConfig tableConfig, TableMetaData tableMetaData) {
        this.tableMetaData = tableMetaData;
        this.tableConfig = tableConfig;

        pkg = tableConfig.getPkgName();
        rootPkg = tableConfig.getPkgName();

        actionClass = tableConfig.getActionClassName();
        entityClass= tableConfig.getEntityClassName();

        serviceClass = tableConfig.getServiceClassName();
        serviceImplClass = tableConfig.getServiceImplClassName();

        daoClass = tableConfig.getDaoClassName();
        daoImplClass = tableConfig.getDaoImplClassName();

        entityVar= Util.decapatilize(entityClass);
        serviceVar= Util.decapatilize(serviceClass);
        daoVar =Util.decapatilize(daoClass);
    }

    public String replaceVar(String content) {

        content = content.replace("${actionClass}", actionClass);

        content = content.replace("${serviceClass}", serviceClass);
        content = content.replace("${serviceImplClass}", serviceImplClass);
        content = content.replace("${serviceVar}", serviceVar);

        content = content.replace("${daoClass}", daoClass);
        content = content.replace("${daoImplClass}", daoImplClass);
        content = content.replace("${daoVar}", daoVar);

        content = content.replace("${entityVar}", entityVar);
        content = content.replace("${entityClass}", entityClass);

        content = content.replace("${rootPkg}", rootPkg);

        return content;
    }

    public File getTemplateFile(String fileName) {
        SpriteConfig spriteConfig = SpriteConfig.getInstance();
        String templatePath = FilenameUtils.concat(spriteConfig.getTemplateDir(),fileName);
        File file = new File(templatePath);
        return file;
    }


}
